package henu.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.opensymphony.xwork2.ActionSupport;

import henu.bean.Schedule;
import henu.bean.Teacher;
import henu.factory.DaoFactory;



public class GetExcelAction extends ActionSupport {
	private File excelFile;
	private String excelFileFileName;

	public File getExcelFile()
	{
		return excelFile;
	}

	public void setExcelFile(File excelFile)
	{
		this.excelFile = excelFile;
	}

	public String getExcelFileFileName()
	{
		return excelFileFileName;
	}

	public void setExcelFileFileName(String excelFileFileName)
	{
		this.excelFileFileName = excelFileFileName;
	}


	private Workbook createWorkBook(InputStream is) throws IOException
	{
		if (excelFileFileName.toLowerCase().endsWith("xls"))
		{
			return new HSSFWorkbook(is);
		}

		if (excelFileFileName.toLowerCase().endsWith("xlsx"))
		{
			return new XSSFWorkbook(is);
		}

		return null;

	}

	public String teacherExcel() throws Exception
	{
		Workbook workbook = createWorkBook(new FileInputStream(excelFile));
		Sheet sheet = workbook.getSheetAt(0);
		Row firstRow = sheet.getRow(0);
		Iterator<Cell> iterator = firstRow.iterator();
		List<String> cellNames = new ArrayList<String>();
		while (iterator.hasNext())
		{
			cellNames.add(iterator.next().getStringCellValue());
		}

		for (int i = 1; i <= sheet.getLastRowNum(); i++)
		{
			System.out.println(3);
			Row row = sheet.getRow(i);
			Teacher tea = new Teacher();
			//一个数字类型的Cell读取出一个字符串并写入数据库时 先设置cell类型
			if(row.getCell(0)!=null){
		          row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
		          tea.setfTeacherId(row.getCell(0).getStringCellValue());   
		     }
			 
			tea.setfTeachName(row.getCell(1).getStringCellValue());  
			tea.setfJobTitle(row.getCell(2).getStringCellValue());  
			tea.setfDept(row.getCell(3).getStringCellValue()); 
			if(row.getCell(4)!=null){
		          row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
		          tea.setfPhoneNumber(row.getCell(4).getStringCellValue());
		     }
			if(row.getCell(5)!=null){
		          row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
		          tea.setfEmail(row.getCell(5).getStringCellValue());
		     }
			if(row.getCell(6)!=null){
		          row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
		          tea.setfQQ(row.getCell(6).getStringCellValue());
		     }
			
			tea.setfMemo(row.getCell(7).getStringCellValue());
			DaoFactory.createTeacherDao().add(tea);
			System.out.println(tea.getfTeachName());
		}
		return "teacher";
	}

	public String scheduleExcel() throws Exception
	{
		Workbook workbook = createWorkBook(new FileInputStream(excelFile));
		Sheet sheet = workbook.getSheetAt(0);
		Row firstRow = sheet.getRow(0);
		Iterator<Cell> iterator = firstRow.iterator();
		List<String> cellNames = new ArrayList<String>();
		while (iterator.hasNext())
		{
			cellNames.add(iterator.next().getStringCellValue());
		}

		for (int i = 1; i <= sheet.getLastRowNum(); i++)
		{
			Row row = sheet.getRow(i);
			Schedule sche = new Schedule();
			//一个数字类型的Cell读取出一个字符串并写入数据库时 先设置cell类型
			if(row.getCell(0)!=null){
		          row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
		          sche.setfCourseNum(row.getCell(0).getStringCellValue());   
		     }
			 
			sche.setfCourseName(row.getCell(1).getStringCellValue());  
			sche.setfCourseYear(row.getCell(2).getStringCellValue());  
			sche.setfCourseTerm(row.getCell(3).getStringCellValue()); 
			if(row.getCell(4)!=null){
		          row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
		          sche.setfGrade(row.getCell(4).getStringCellValue());
		     }
			DaoFactory.createScheduleDao().add(sche);
			System.out.println(sche.getfCourseName());
		}
		return "schedule";
	}
}